package cn.laixueit.aop;

import cn.laixueit.annotation.GlobalLog;
import cn.laixueit.service.CategoryService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Method;

/**
 * @author duxiang
 * @date 2024/1/10 13:59
 */
@Aspect
@Component
public class LogAop {

    @Resource
    private CategoryService categoryService;

    @Pointcut("@annotation(cn.laixueit.annotation.GlobalLog)")
    public void pointcut(){

    }

    @Before("pointcut() && @annotation(globalLog)")
    public void around(JoinPoint point, GlobalLog globalLog) throws Throwable {
        Signature signature = point.getSignature();//签名
        Object target = point.getTarget();//类
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = target.getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
        System.out.println("method.getName() = " + method.getName());
        System.out.println("保存日志");
        System.out.println("globalLog.value() = " + globalLog.value());
        System.out.println(globalLog.description());
        System.out.println(categoryService.list());
    }

    /*@Around("pointcut() && @annotation(globalLog)")
    public Object around(ProceedingJoinPoint point, GlobalLog globalLog) throws Throwable {
        Object[] args = point.getArgs();
        Object proceed = point.proceed(args);
        Signature signature = point.getSignature();//签名
        Object target = point.getTarget();//类
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = target.getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
        System.out.println("method.getName() = " + method.getName());
        proceed.getClass();
        System.out.println("保存日志");
        System.out.println("globalLog.value() = " + globalLog.value());
        System.out.println(globalLog.description());
        System.out.println(categoryService.list());
        return  proceed;
    }*/
}
